package com.tangtang.core;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestUtil {

    /**
     * 获取请求
     *
     * @return HttpServletRequest
     */
    public static HttpServletRequest getRequest() {
        // for spring mvc
        // 在web。xml中配置一个监听
        // <listener>
        // <listener-class>
        // org.springframework.web.context.request.RequestContextListener
        // </listener-class>
        // </listener>
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
        // for struts2
        // HttpServletRequest request = ServletActionContext.getRequest();
        // return request;
    }


    /**
     * 获取ip地址
     *
     * @return
     */
    public static String getIpAddr() {
        HttpServletRequest request = getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }


    /**
     * 是否是内网ip
     *
     * @param ipAddress
     * @return
     */
    public static boolean isInnerIP(String ipAddress) {
        boolean isInnerIp = false;
        long ipNum = getIpNum(ipAddress);
        /**
         * 私有IP：A类 10.0.0.0-10.255.255.255 B类 172.16.0.0-172.31.255.255 C类
         * 192.168.0.0-192.168.255.255 当然，还有127这个网段是环回地址
         **/
        long aBegin = getIpNum("10.0.0.0");
        long aEnd = getIpNum("10.255.255.255");
        long bBegin = getIpNum("172.16.0.0");
        long bEnd = getIpNum("172.31.255.255");
        long cBegin = getIpNum("192.168.0.0");
        long cEnd = getIpNum("192.168.255.255");
        isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd) || ipAddress.equals("127.0.0.1");
        return isInnerIp;
    }


    /**
     * 将ip转化为long
     *
     * @param ipAddress
     * @return
     */
    private static long getIpNum(String ipAddress) {
        String[] ip = ipAddress.split("\\.");
        long a = Integer.parseInt(ip[0]);
        long b = Integer.parseInt(ip[1]);
        long c = Integer.parseInt(ip[2]);
        long d = Integer.parseInt(ip[3]);

        long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
        return ipNum;
    }


    private static boolean isInner(long userIp, long begin, long end) {
        return (userIp >= begin) && (userIp <= end);
    }

    /**
     * 获取参数转化为int
     *
     * @param key
     * @return
     */
    public static int getInt(String key) {
        return getInt(key, 0);
    }

    /**
     * 获取参数转化为int
     *
     * @param key
     * @param defaultVal
     * @return
     */
    public static int getInt(String key, int defaultVal) {
        return getInt(getRequest(), key, defaultVal);
    }

    /**
     * @param request
     * @param key
     * @param defaultVal
     * @return
     */
    public static int getInt(HttpServletRequest request, String key, int defaultVal) {
        String param = getString(request, key);
        return ConvertUtil.StrToInt(param, defaultVal);
    }

    /**
     * 获取参数转化为long
     *
     * @param key
     * @return
     */
    public static long getLong(String key) {
        return getInt(key, 0);
    }

    /**
     * 获取参数转化为long
     *
     * @param key
     * @param defaultVal
     * @return
     */
    public static long getLong(String key, long defaultVal) {
        return getLong(getRequest(), key, defaultVal);
    }


    /**
     * 获取参数转化为long
     *
     * @param request
     * @param key
     * @param defaultVal
     * @return
     */
    public static long getLong(HttpServletRequest request, String key, long defaultVal) {
        String param = getString(request, key);
        return ConvertUtil.StrToLong(param, defaultVal);
    }

    /**
     * 获取参数
     *
     * @param key
     * @return
     */
    public static String getString(String key) {
        return getString(getRequest(), key);
    }

    /**
     * @param request
     * @param key
     * @return
     */
    public static String getString(HttpServletRequest request, String key) {
        String value = request.getParameter(key);
        if (StringUtil.isNotBlank(value)) {
            return value.trim();
        }
        return null;
    }

    /**
     * 获取统一参数的数组
     *
     * @param request
     * @param key
     * @return
     */
    public static String[] getStrings(HttpServletRequest request, String key) {
        String[] value = request.getParameterValues(key);
        if (value != null && value.length > 0) {
            for (int i = 0; i < value.length; i++) {
                value[i] = StringUtil.trim(value[i]);
            }
            return value;
        }
        return null;
    }

    /**
     * 获取同一参数的数组
     *
     * @param request
     * @param key
     * @return
     */
    public static int[] getInts(HttpServletRequest request, String key, int defaultVal) {
        String[] value = request.getParameterValues(key);

        if (value != null && value.length > 0) {
            int[] result = new int[value.length];
            for (int i = 0; i < value.length; i++) {
                result[i] = ConvertUtil.StrToInt(value[i], defaultVal);
            }
            return result;
        }
        return null;
    }

    /**
     * 获取同一参数的数组
     *
     * @param request
     * @param key
     * @return
     */
    public static long[] getLongs(HttpServletRequest request, String key, long defaultVal) {
        String[] value = request.getParameterValues(key);

        if (value != null && value.length > 0) {
            long[] result = new long[value.length];
            for (int i = 0; i < value.length; i++) {
                result[i] = ConvertUtil.StrToLong(value[i], defaultVal);
            }
            return result;
        }
        return null;
    }
}
